package com.carbon.assets.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carbon.assets.entity.CarbonCreditAssets;
import com.carbon.assets.service.CarbonQuotaAssetsService;
import com.carbon.assets.entity.CarbonQuotaAssets;
import com.carbon.assets.common.BaseController;
import com.carbon.assets.service.DictService;
import com.carbon.assets.vo.CarbonCreditAssetsQueryVo;
import com.carbon.assets.vo.CarbonQuotaAssetsQueryVo;
import com.carbon.assets.vo.SelectorVo;
import com.carbon.domain.common.ApiResult;
import com.carbon.trade.entity.CarbonTradeQuote;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * 碳配额资产 前端控制器
 * </p>
 *
 * @author Li Jun
 * @since 2022-04-24
 */
@Slf4j
@RestController
@RequestMapping("/carbonQuotaAssets")
@Api(value = "碳配额资产模块", tags = {"碳配额资产模块"})
public class CarbonQuotaAssetsController extends BaseController {

    @Resource CarbonQuotaAssetsService carbonQuotaAssetsService;
    @Resource DictService dictService;
    @GetMapping("/queryAllQuotaAssets")
    public ApiResult queryAllQuotaAssets(@RequestParam String tenantId) {
        // 构建查询条件，按租户ID筛选
        LambdaQueryWrapper<CarbonQuotaAssets> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CarbonQuotaAssets::getTenantId, tenantId);
        // 查询所有符合条件的数据列表
        List<CarbonQuotaAssets> assetsList = carbonQuotaAssetsService.list(queryWrapper);
        for (CarbonQuotaAssets carbonQuotaAssets : assetsList) {
            carbonQuotaAssets.setAssetsStatus(dictService.getDictCh(carbonQuotaAssets.getAssetsStatus()));
        }
        return ApiResult.ok(assetsList);
    }


    @GetMapping("/queryQuotaAssetsSum")
    public ApiResult queryQuotaAssetsSum(@RequestParam String tenantId) {
        System.out.println( "--------------------------后端进入-----------------------------");
        LambdaQueryWrapper<CarbonQuotaAssets> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CarbonQuotaAssets::getTenantId, tenantId);
        List<CarbonQuotaAssets> list = carbonQuotaAssetsService.list(queryWrapper);
        BigDecimal totalSum = BigDecimal.ZERO;
        BigDecimal availableAmountSum = BigDecimal.ZERO;
        BigDecimal frozenAmountSum = BigDecimal.ZERO;
        BigDecimal lockedAmountSum = BigDecimal.ZERO;
        for (CarbonQuotaAssets carbonQuotaAssets : list) {
            totalSum = totalSum.add(carbonQuotaAssets.getTotal() != null ? carbonQuotaAssets.getTotal() : BigDecimal.ZERO);
            availableAmountSum = availableAmountSum.add(carbonQuotaAssets.getAvailableAmount() != null ? carbonQuotaAssets.getAvailableAmount() : BigDecimal.ZERO);
            frozenAmountSum = frozenAmountSum.add(carbonQuotaAssets.getFrozenAmount() != null ? carbonQuotaAssets.getFrozenAmount() : BigDecimal.ZERO);
            lockedAmountSum = lockedAmountSum.add(carbonQuotaAssets.getLockedAmount() != null ? carbonQuotaAssets.getLockedAmount() : BigDecimal.ZERO);
        }
        CarbonQuotaAssetsQueryVo carbonQuotaAssetsQueryVo = new CarbonQuotaAssetsQueryVo();
        carbonQuotaAssetsQueryVo.setTotal(totalSum);
        carbonQuotaAssetsQueryVo.setAvailableAmount(availableAmountSum);
        carbonQuotaAssetsQueryVo.setFrozenAmount(frozenAmountSum);
        carbonQuotaAssetsQueryVo.setLockedAmount(lockedAmountSum);
        return ApiResult.ok(carbonQuotaAssetsQueryVo);
    }

    @GetMapping("/queryBySelector")
    public ApiResult queryBySelector(SelectorVo selectorVo) {
        // 1. 验证必填参数
        if (selectorVo.getTenantId() == null || selectorVo.getTenantId().isEmpty()) {
            return ApiResult.fail("租户ID不能为空");
        }
        // 2. 创建分页对象（使用Vo中的分页参数，默认第一页，每页10条）
        Page<CarbonQuotaAssets> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(
                selectorVo.getPageNum(),
                selectorVo.getPageSize()
        );
        // 3. 调用服务层的分页查询方法
        Page<CarbonQuotaAssetsQueryVo> resultPage =
                carbonQuotaAssetsService.selectBySelector(page, selectorVo);

        // 4. 处理分页结果中的状态字典转换
        for (CarbonQuotaAssetsQueryVo queryVo : resultPage.getRecords()) {
            queryVo.setAssetsStatus(dictService.getDictCh(queryVo.getAssetsStatus()));
        }
        // 5. 返回包含分页信息的结果
        return ApiResult.ok(resultPage);
    }

    @PostMapping("/upload")
    public ApiResult upload(@RequestBody CarbonQuotaAssets carbonQuotaAssets) {
        carbonQuotaAssets.setAvailableAmount(carbonQuotaAssets.getTotal());
        carbonQuotaAssets.setValuation(carbonQuotaAssets.getTotal().multiply(BigDecimal.valueOf(50)));
        System.out.println("---------------------这里是upload"+carbonQuotaAssets.toString());
       carbonQuotaAssetsService.save(carbonQuotaAssets);
        return ApiResult.ok();
    }


    @GetMapping("/queryQuotaAssets")
    public ApiResult queryCreditAssets(@RequestParam String tenantId,String id) {
        LambdaQueryWrapper<CarbonQuotaAssets> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CarbonQuotaAssets::getTenantId, tenantId);
        wrapper.eq(CarbonQuotaAssets::getId, id);
        // 查询所有符合条件的数据列表
        CarbonQuotaAssets carbonQuotaAssets = carbonQuotaAssetsService.getOne(wrapper);
        carbonQuotaAssets.setAssetsStatus(dictService.getDictCh(carbonQuotaAssets.getAssetsStatus()));
        System.out.println("这是一个queryQuotaAssets里的输出"+carbonQuotaAssets.toString());
        return ApiResult.ok(carbonQuotaAssets);
    }

    @PostMapping("/updateAmount")
    ApiResult updateAmount(@RequestBody CarbonTradeQuote carbonTradeQuote){
        carbonQuotaAssetsService.updateAmount(carbonTradeQuote.getProjectId(),carbonTradeQuote.getTradeQuantity());
        return ApiResult.ok();
    }
    @GetMapping("/queryTotal/{projectId}")
    ApiResult<BigDecimal> queryTotal(@PathVariable("projectId") Long projectId){
        System.out.println("-------------------------这里是queryTotal");
        return ApiResult.ok(carbonQuotaAssetsService.getByCarbonProjectId(projectId).getAvailableAmount());
    }

}

